package com.caigou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.caigou.bean.cms.entity.Copyright;
import com.caigou.bean.cms.param.CopyrightParam;
import com.caigou.component.HtmlBuilderComponent;
import com.caigou.mapper.cms.CopyrightMapper;
import com.caigou.service.CopyrightService;
import com.caigou.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service("globalConfigService")
public class CopyrightServiceImpl implements CopyrightService {

    @Resource
    private CopyrightMapper copyrightMapper;

    @Resource
    private HtmlBuilderComponent htmlBuilderComponent;

    public List<Copyright> listCopyright(){
        return copyrightMapper.listCopyright();
    }

    public Result saveAndPublishCopyrightStatement(CopyrightParam param){
        Copyright copyright = new Copyright();
        BeanUtil.copyProperties(param, copyright);
        int result = copyrightMapper.updateCopyrightByCode(copyright);
        if(result>0){
            //生成版权声明碎片
            htmlBuilderComponent.buildCopyrightHtml(copyright);
            return Result.success("修改版权声明成功");
        }else{
            return Result.error("修改版权声明失败");
        }
    }



    public Result updateTortImage(CopyrightParam param){
        Copyright copyright = new Copyright();
        BeanUtil.copyProperties(param, copyright);
        int result = copyrightMapper.updateCopyrightByCode(copyright);
        if(result>0){
            return Result.success("修改反侵权图片成功");
        }else{
            return Result.error("修改反侵权图片失败");
        }
    }

    public Result saveAndPublishTortAnnouncement(CopyrightParam param){
        Copyright copyright = new Copyright();
        BeanUtil.copyProperties(param, copyright);
        int result = copyrightMapper.updateCopyrightByCode(copyright);
        if(result>0){
            //TOTO
            //生成反侵权图片碎片及页面
            Copyright imageCopyright = copyrightMapper.selectByCopyrightCode("tortImage");
            Copyright announcementCopyright = copyrightMapper.selectByCopyrightCode("tortAnnouncement");
            String tortUrl = htmlBuilderComponent.buildTortHtml(imageCopyright, announcementCopyright);
            log.info("反侵权公告url={}", tortUrl);
            return Result.success("保存并发布反侵权图片、公告成功");
        }else{
            return Result.error("保存并发布反侵权图片、公告失败");
        }

    }

    public Copyright getCopyrightByCopyrightCode(String copyrightCode){
        return copyrightMapper.selectByCopyrightCode(copyrightCode);
    }
}
